package com.ruoyi.common.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.group.entity.YytGroupSkuOrderRelation;
import com.ruoyi.common.group.dao.YytGroupSkuOrderRelationMapper;
import com.ruoyi.common.group.entity.YytGroupSkuRelation;
import com.ruoyi.common.group.service.YytGroupSkuOrderRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.group.service.YytGroupSkuRelationService;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.oms.entity.YytOmsOrderItem;
import com.ruoyi.common.oms.service.YytOmsOrderItemService;
import com.ruoyi.common.oms.service.YytOmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* <p>
    *  服务实现类
    * </p>
*
* @author MrPing
* @since 2022-07-19
*/
@Service
public class YytGroupSkuOrderRelationServiceImpl extends ServiceImpl<YytGroupSkuOrderRelationMapper, YytGroupSkuOrderRelation> implements YytGroupSkuOrderRelationService {

    @Autowired
    private YytGroupSkuOrderRelationMapper yytGroupSkuOrderRelationMapper;

    @Autowired
    private YytGroupSkuRelationService yytGroupSkuRelationService;

    @Autowired
    private YytOmsOrderService yytOmsOrderService;

    @Autowired
    private YytOmsOrderItemService yytOmsOrderItemService;

    /**
     * 判断是否是拼团商品
     * @param orderId
     */
    @Override
    public void isGroupOrder(String orderId) {
        YytOmsOrder omsOrder = yytOmsOrderService.getById(orderId);

        // 如果是拼团商品
        if(omsOrder.getOrderType().equals("3")){
            QueryWrapper<YytOmsOrderItem> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", orderId);
            YytOmsOrderItem orderItem = yytOmsOrderItemService.getOne(wrapper, false);
            QueryWrapper<YytGroupSkuRelation> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("sku_id", orderItem.getSkuId());
            YytGroupSkuRelation skuRelation = yytGroupSkuRelationService.getOne(wrapper1, false);
            // 添加配送日期
            UpdateWrapper<YytOmsOrder> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", orderId).set("delivery_date", skuRelation.getSendDate());
            yytOmsOrderService.update(updateWrapper);
            // 插入拼团商品订单表
            YytGroupSkuOrderRelation yytGroupSkuOrderRelation = new YytGroupSkuOrderRelation();
            yytGroupSkuOrderRelation.setGroupId(skuRelation.getGroupId());
            yytGroupSkuOrderRelation.setGroupSkuId(skuRelation.getSkuId());
            yytGroupSkuOrderRelation.setOrderId(orderId);
            super.save(yytGroupSkuOrderRelation);
            // 修改拼团商品表数据
            if(skuRelation.getGroupNum() - 1 == skuRelation.getYetNum()) {
                skuRelation.setIsSuccess(1);
            }
            skuRelation.setYetNum(skuRelation.getYetNum() + 1);
            yytGroupSkuRelationService.updateById(skuRelation);
        }
    }
}
